using UnityEngine;
using TMPro;

public class EnemyWaveUI : MonoBehaviour
{
    [SerializeField] private EnemyWaveManager enemyWaveManager; // 敌人波次管理器

    private TextMeshProUGUI waveNumberText; // 显示波次编号的文本
    private TextMeshProUGUI waveMessageText; // 显示波次消息的文本
    private RectTransform enemyWaveSpawnPositionIndicator;//敌人指示器
    private Camera mainCamera;

    private void Awake()
    {
        // 获取波次编号文本和波次消息文本的引用
        waveNumberText = transform.Find("波数").GetComponent<TextMeshProUGUI>();
        waveMessageText = transform.Find("计时器").GetComponent<TextMeshProUGUI>();
        enemyWaveSpawnPositionIndicator = transform.Find("敌人指示器").GetComponent<RectTransform>();

        mainCamera = Camera.main;
    }

    private void Start()
    {
        // 订阅敌人波次管理器的波次编号变化事件
        enemyWaveManager.OnWaveNumberChanged += EnemyWaveManager_OnWaveNumberChanged;
    }

    private void EnemyWaveManager_OnWaveNumberChanged(object sender, System.EventArgs e)
    {
        // 当波次编号发生变化时，更新波次编号文本显示
        SetWaveNumberText("波数 " + enemyWaveManager.GetWaveNumber());
    }

    private void Update()
    {
        float nextWaveSpawnTimer = enemyWaveManager.GetNextWaveSpawnTimer();

        if (nextWaveSpawnTimer < 0f)
        {
            // 如果下一波次生成的计时器小于0，则清空波次消息文本
            SetMessageText("");
        }
        else
        {
            // 显示下一波次生成的计时器
            SetMessageText("下波倒计时 " + nextWaveSpawnTimer.ToString("F1") + "s");
        }

        Vector3 dirToNextSpawnPosition = (enemyWaveManager.GetSpawnPosition() - mainCamera.transform.position).normalized;
        enemyWaveSpawnPositionIndicator.anchoredPosition = dirToNextSpawnPosition * 300f;
        enemyWaveSpawnPositionIndicator.eulerAngles = new Vector3(0, 0, UtilsClass.GetAngleFromVector(dirToNextSpawnPosition));
        float distanceToNextspawnPosition = Vector3.Distance(enemyWaveManager.GetSpawnPosition(), mainCamera.transform.position);
        enemyWaveSpawnPositionIndicator.gameObject.SetActive(distanceToNextspawnPosition > mainCamera.orthographicSize * 1.5f);
    }

    private void SetMessageText(string message)
    {
        // 更新波次消息文本显示
        waveMessageText.SetText(message);
    }

    private void SetWaveNumberText(string text)
    {
        // 更新波次编号文本显示
        waveNumberText.SetText(text);
    }
}
